<template>
  <div class="h100" ref="projectTableList">
    <s-table ref="table" class="h100" :rowKey="record => record.id" :columns="columns" :data="loadData"
      @getPagination="getPagination">
      <template slot="systemName" slot-scope="text">
        <table-ellipsis :text="getSystemName(text)"></table-ellipsis>
      </template>
      <template slot="handleType" slot-scope="text">
        <table-ellipsis v-if="text == 0" text="重传"></table-ellipsis>
        <table-ellipsis v-else text="同步"></table-ellipsis>
      </template>
      <template slot="syncTotal" slot-scope="text">
        <table-ellipsis :text="`${text}条`"></table-ellipsis>
      </template>
      <template slot="handleTime" slot-scope="text">
        <table-ellipsis :text="new Date(text).toLocaleString().replace(/\//g, '-')"></table-ellipsis>
      </template>
      <template slot="handleBy" slot-scope="text">
        <table-ellipsis :text="text"></table-ellipsis>
      </template>
      <template slot="status" slot-scope="text">
        <table-ellipsis v-if="text == 0" :text="$t('fail')"></table-ellipsis>
        <table-ellipsis v-else-if="text == 1" :text="$t('success')"></table-ellipsis>
        <table-ellipsis v-else-if="text == 2" :text="$t('retransmission')"></table-ellipsis>
      </template>
      <template slot="remark" slot-scope="text">
        <span>{{ text.includes('同步的时间为') ? text : `同步的时间为${text}` }}</span>
      </template>
      <template slot="operation" slot-scope="text,record">
        <div class="sys-log-btns">
          <auth-button size="small" icon="xiazai" @click="download(record)">
            <i class="iconfont icon-xiazai" />
            {{ $t('download') }}
          </auth-button>
          <auth-button size="small" @click="handleRetransmission(record)">
            <i class="iconfont icon-zhengzaijinxing" />
            {{ $t('retransmission') }}
          </auth-button>
        </div>
      </template>
    </s-table>
  </div>
</template>
<script>
import syncLogApi from '@/api/modules/system/syncLog';
import { STable } from '@/components';
import AuthButton from '@/components/AuthButton';
import AuthText from '@/components/AuthText';
import tableEllipsis from '@/components/Ellipsis/TableEllipsis';
import { managePerms } from '@/config/permission/index';
import { downloadBlobFile } from '@/utils/functions';
import { mapState } from 'vuex';
import { sysLogTbColumns } from '../config/list';
export default {
  components: {
    tableEllipsis,
    STable,
    AuthText,
    AuthButton
  },
  computed: {
    ...mapState({
      queryParams: (state) => state.syncLog.queryParams,
      order: (state) => state.syncLog.order
    }),
    commonColumns() {
      return this.columns.filter(item => !item.special)
    }
  },
  data() {
    return {
      columns: sysLogTbColumns,
      managePerms
    }
  },
  watch: {
  },
  created() {
  },
  mounted() {
  },
  methods: {
    loadData(parameter) {
      this.$store.commit('setOrder', parameter.order);
      console.log(parameter, 'parameter');
      const params = { ...parameter, ...this.queryParams };
      for (const key in params) {
        if (typeof params[key] === 'undefined') {
          params[key] = '';
        }
      }
      console.log(params, 'params');
      // if (params.startTime) {
      //   params.startTime = params.startTime + ':00'
      // }
      // if (params.endTime) {
      //   params.endTime = params.endTime + ':00'
      // }
      return syncLogApi.getSysLogPageData(params)
    },
    doAction(key, rowData) {
      if (this[key]) {
        this[key](rowData)
        return
      }
      this.$emit('clickbtn', { type: key, data: rowData })
    },
    getPagination() {

    },
    refreshData(isToFirstPage = true) {
      console.log(isToFirstPage, 'isToFirstPage');
      const vm = this
      vm.$nextTick(() => {
        vm.$refs.table.refresh(isToFirstPage)
      })
    },
    handleRetransmission(record) {
      const vm = this;
      this.$confirm({
        title: vm.$t('retransmission'),
        content: '确认后将立即重新传输此次同步的数据',
        class: 'confirmStyle',
        icon: '<Icon >',
        width: 378,
        onOk: vm.retransmission,
        onCancel() { }
      });
    },
    retransmission() {
      const currentDate = new Date();
      const nextDate = new Date(currentDate.getTime() + 1000 * 60 * 60 * 24);
      syncLogApi.retransmission({
        fromDate: currentDate.toLocaleDateString().replace(/\//g, '-'),
        toDate: nextDate.toLocaleDateString().replace(/\//g, '-')
      }).then(res => {
        console.log(res, 'res');
      })
    },
    getSystemName(systemName) {
      switch (systemName) {
        case 'MES':
          return '生产管理系统数据源';
        case 'CLOUD':
          return '云服务数据源';
        case 'GCY':
          return '工程院数据源';
        case 'PRODUCT':
          return '产品管理平台数据源';
      }
    },
    download(record) {
      syncLogApi.downFile(record.note).then(res => {
        console.log(typeof res, 'type');
        const fileName = this.getSystemName(record.systemName);
        downloadBlobFile(res, `${fileName}.xlsx`);
      });
    }
  }
}
</script>
<style lang="less" scoped>
.sys-log-btns {
  display: flex;
  justify-content: space-around;
  align-items: center;

  button {
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
